﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using Bitis_KeToanThue_Pro_Update.AccessDatabase;
using Bitis_KeToanThue_Pro_DungChung.Common;
using Bitis_KeToanThue_Pro_Update.Properties;
using Bitis_KeToanThue_Pro_Update.Common;

namespace Bitis_KeToanThue_Pro_Update.Form
{
    public partial class frmLogin : Telerik.WinControls.UI.RadForm
    {
        public event DangNhapThanhCongHandler DanhNhapThanhCong = null;
        DangNhap dn = new DangNhap();
        public frmLogin()
        {
            InitializeComponent();
            this.Text = "Login " + BienHeThong.ht_tenchuongtrinh;
        }
        private void soanlai()
        {
            txtMatKhau.Text = "";
            txtUser.Text = "";
        }

        private void checkdangnhap(int vitri)
        {

            //txtUserName.Focus();
            if (txtMatKhau.Text != "" && txtUser.Text != "")
            {
                DataTable dt = new DataTable();
                dt = dn.logininformation(txtUser.Text.Trim(), txtMatKhau.Text.Trim(), BienHeThong.ht_machuongtrinh, vitri);

                if (dt.Rows.Count > 0)
                {

                    if (chkNho.Checked == true)
                    {
                        Settings.Default.UserIdSetting = txtUser.Text;
                        Settings.Default.PasswordSetting = txtMatKhau.Text;

                    }
                    else
                    {
                        Settings.Default.UserIdSetting = string.Empty;
                        Settings.Default.PasswordSetting = string.Empty;
                    }
                    Settings.Default.CheckBookSeting = chkNho.Checked;

                    Settings.Default.Save();

                    BienHeThong.ht_userid = ClassXuLyChung.GetSafeInt(dt.Rows[0]["UserID"]);
                    BienHeThong.ht_manv = ClassXuLyChung.GetSafeString(dt.Rows[0]["MaNV"]);
                    BienHeThong.ht_tennv = ClassXuLyChung.GetSafeString(dt.Rows[0]["TenNV"]);
                    BienHeThong.ht_DoUuTienUser = ClassXuLyChung.GetSafeInt(dt.Rows[0]["DoUuTienUser"]);
                    BienHeThong.ht_MaPB = ClassXuLyChung.GetSafeString(dt.Rows[0]["MaPB"]);
                    BienHeThong.ht_TenPB = ClassXuLyChung.GetSafeString(dt.Rows[0]["TenPB"]);
                    BienHeThong.ht_MaChucVu = ClassXuLyChung.GetSafeInt(dt.Rows[0]["MaChucVu"]);
                    BienHeThong.ht_ChucVu = ClassXuLyChung.GetSafeString(dt.Rows[0]["ChucVu"]);
                    BienHeThong.ht_MaNhomUser = ClassXuLyChung.GetSafeInt(dt.Rows[0]["MaNhomUser"]);
                    BienHeThong.ht_DoUuTienNhom = ClassXuLyChung.GetSafeInt(dt.Rows[0]["DoUuTienNhom"]);
                    BienHeThong.ht_MaChuongTrinhLoginSuseccFull = ClassXuLyChung.GetSafeInt(dt.Rows[0]["MaChuongTrinh"]);
                    BienHeThong.ht_IDViTri = ClassXuLyChung.GetSafeInt(dt.Rows[0]["IDViTri"]);
                    BienHeThong.ht_MaNhomMenuND = ClassXuLyChung.GetSafeInt(dt.Rows[0]["MaNhomMenuND"]);
                    if (DanhNhapThanhCong != null)
                    {
                        //  UpdateBienBanEventArgs args = new UpdateBienBanEventArgs(txtBienBan.Text.Trim());
                        DanhNhapThanhCong(this, new DangNhapThanhCongEventArgs
                        {
                            dl_userid = ClassXuLyChung.GetSafeInt(dt.Rows[0]["UserID"]),
                            dl_manv = ClassXuLyChung.GetSafeString(dt.Rows[0]["MaNV"]),
                            dl_tennv = ClassXuLyChung.GetSafeString(dt.Rows[0]["TenNV"]),
                            dl_DoUuTienUser = ClassXuLyChung.GetSafeInt(dt.Rows[0]["DoUuTienUser"]),
                            dl_MaPB = ClassXuLyChung.GetSafeString(dt.Rows[0]["MaPB"]),
                            dl_TenPB = ClassXuLyChung.GetSafeString(dt.Rows[0]["TenPB"]),
                            dl_MaChucVu = ClassXuLyChung.GetSafeInt(dt.Rows[0]["MaChucVu"]),
                            dl_ChucVu = ClassXuLyChung.GetSafeString(dt.Rows[0]["ChucVu"]),
                            dl_MaNhomUser = ClassXuLyChung.GetSafeInt(dt.Rows[0]["MaNhomUser"]),
                            dl_DoUuTienNhom = ClassXuLyChung.GetSafeInt(dt.Rows[0]["DoUuTienNhom"]),
                            dl_MaChuongTrinhLoginSuseccFull = ClassXuLyChung.GetSafeInt(dt.Rows[0]["MaChuongTrinh"]),
                            dl_IDViTri = ClassXuLyChung.GetSafeInt(dt.Rows[0]["IDViTri"]),
                            dl_MaNhomMenuND = ClassXuLyChung.GetSafeInt(dt.Rows[0]["MaNhomMenuND"]),
                        });
                        //chay su kien bienbanupdated

                    }
                    soanlai();
                    RadMessageBox.Show("Đăng nhập thành công", "Thông Báo", MessageBoxButtons.OK, RadMessageIcon.Info);





                    ShowFormMenuMainProject.openformcloseformfinal("Bitis_KeToanThue_Pro_Update.Form.frmMain", "frmMain", this);



                    // this.Close();


                }
                else
                {
                    RadMessageBox.Show("Sai tên đăng nhập hoặc mật khẩu", "Thông Báo", MessageBoxButtons.OK, RadMessageIcon.Error);
                }

            }
            else
            {
                RadMessageBox.Show("Bạn chưa nhập tên đăng nhập hoặc mật khẩu", "Thông Báo", MessageBoxButtons.OK, RadMessageIcon.Exclamation);
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {

            if (Application.OpenForms.Count == 1)
            {
                ShowFormMenuMainProject.openformcloseformfinal("Bitis_KeToanThue_Pro_Update.Form.frmManHinhChinh", "frmManHinhChinh", this);
            }
            else
            {
                this.Close();
            }
        }

        private void txtUser_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtMatKhau.Focus();
            }
        }

        private void txtMatKhau_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                checkdangnhap(1);
                //txtMatKhau.Focus();
            }
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {


            txtMatKhau.Text = Settings.Default.PasswordSetting;
            txtUser.Text = Settings.Default.UserIdSetting;
            chkNho.Checked = Settings.Default.CheckBookSeting;
        }

        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            checkdangnhap(1);
        }
    }
}
